package cn.quevo.core.file.excel;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import cn.quevo.core.lang.StringUtils;

/**
 * Excel写入
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 15:59:08
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public class ExcelWrite {

    /**
     * 创建空白的Excel工作簿
     *
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:59:34
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Workbook createWorkBook() {
        return new HSSFWorkbook();
    }

    /**
     * 创建Sheet
     *
     * @param workbook
     * @param sheetName
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:59:46
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Sheet createSheet(Workbook workbook, String sheetName) {
        return workbook.createSheet(sheetName);
    }

    /**
     * 创建标题
     *
     * @param workbook
     * @param sheetName
     * @param titles
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:59:53
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Sheet createSheet(Workbook workbook, String sheetName, List<String> titles) {
        Sheet sheet = createSheet(workbook, sheetName);
        Row row = sheet.createRow(0);
        int colNum = 0;
        for (String title : titles) {
            row.createCell(colNum++).setCellValue(title);
        }
        return sheet;
    }

    /**
     * 从文档末尾追加
     *
     * @param sheet
     * @param cols
     * @param dataList
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:00:00
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Sheet appendRow(Sheet sheet, List<String> cols, List<Map<String, Object>> dataList) {
        return appendRow(sheet, cols, dataList, sheet.getLastRowNum());
    }

    /**
     * 追加数据列
     *
     * @param sheet
     * @param cols
     * @param dataList
     * @param rowNum
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:00:07
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Sheet appendRow(Sheet sheet, List<String> cols, List<Map<String, Object>> dataList, int rowNum) {
        int colNum = 0;
        Row row = null;
        for (Map<String, Object> data : dataList) {
            row = sheet.createRow(rowNum++);
            colNum = 0;
            for (String col : cols) {
                row.createCell(colNum++).setCellValue(StringUtils.isBlank(data.get(col)) ? "" : data.get(col).toString());
            }
        }
        return sheet;
    }
}
